﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    
    public partial class Promotion_user : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        int point_distance, promotion_distance, sum_distance=0, sum_choose;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Int32.Parse(Session["login_Access_id"].ToString()) == 2)
                {
                    int i = 0;
                    foreach (GridViewRow gr in GridView1.Rows)
                    {
                        CheckBox CheckBox1 = (CheckBox)gr.FindControl("CheckBox1");
                        if (CheckBox1.Checked == true)
                        {

                            sum_choose += Int32.Parse(GridView1.Rows[i].Cells[3].Text);

                        }
                        i++;
                        Session["sum_choose"] = sum_choose;
                    }
                    TextBox5.Text = sum_choose.ToString();
                    try
                    {
                        TextBox6.Text = (Int32.Parse(Session["sum_distance"].ToString()) - sum_choose).ToString();
                    }
                    catch (Exception)
                    {
                        TextBox6.Text = (0 - sum_choose).ToString();
                    }
                    try
                    {
                        TextBox6.Text = (Int32.Parse(Session["sum_distance"].ToString()) - sum_choose).ToString();
                    }
                    catch (Exception)
                    {                        
                        
                    }
                    
                }
                else
                {
                    Response.Redirect("login.aspx");
                } 
            }
            catch (Exception)
            {
                Response.Redirect("login.aspx");
            }
                       
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session.Remove("sum_distance");
            Session.Remove("sum_choose");
            Session.Remove("user");  
            try
            {
                string check = @"select *
                                from Table_Passenger
                                where Passenger_identification = '" + TextBox1.Text + "'";               
                SqlCommand sqlcomm = new SqlCommand();
                string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                SqlConnection sqlconn = new SqlConnection(Connection);
                SqlDataAdapter da = new SqlDataAdapter(check, sqlconn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count <= 0)
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่มีหมายเลขบัตรประชาชนนี้ในระบบ');", true);
                }
                else
                {
                    Panel2.Visible = true;
                    TextBox2.Text = dt.Rows[0]["Passenger_name"].ToString();
                    TextBox3.Text = dt.Rows[0]["Passenger_surname"].ToString();
                    TextBox5.Text = "0";
                    int user = Int32.Parse(dt.Rows[0]["Passenger_id"].ToString());
                    Session["user"] = user;
                    string distance = @"select Point_distance
                                from Table_Sale
                                inner join Table_Around
                                    on Table_Around.Around_id = Table_Sale.Around_id
                                inner join Table_Route
                                    on Table_Around.Route_id = Table_Route.Route_id
                                inner join Table_Passenger
                                    on Table_Sale.Passenger_id = Table_Passenger.Passenger_id
                                inner join Table_Point
                                    on Table_Sale.Point_id = Table_Point.Point_id                                
                                where Table_Passenger.Passenger_id = '" + user + "' and Table_Sale.SSID = 1";
                    string promotion_item = @"select Table_promotion_item.promotion_item_distance
                                    from Table_Passenger
                                    inner join Table_store_user
                                    on Table_Passenger.Passenger_id = Table_store_user.Passenger_id
                                    inner join Table_promotion_item
                                    on Table_promotion_item.promotion_item_id = Table_store_user.promotion_item_id
                                    where Table_Passenger.Passenger_id = '" + user + "' and Table_promotion_item.promotion_item_status = 1";
                    SqlCommand sqlcomm2 = new SqlCommand();
                    string Connection2 = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                    SqlConnection sqlconn2 = new SqlConnection(Connection2);
                    SqlDataAdapter da2 = new SqlDataAdapter(distance, sqlconn2);
                    SqlDataAdapter da3 = new SqlDataAdapter(promotion_item, sqlconn2);
                    DataTable dt2 = new DataTable();
                    DataTable dt3 = new DataTable();
                    da2.Fill(dt2);
                    da3.Fill(dt3);
                    if (dt2.Rows.Count <= 0)
                    {
                        point_distance = 0;
                    }
                    else
                    {
                        for (int i = 0; i < dt2.Rows.Count; i++)
                        {
                            point_distance += Int32.Parse(dt2.Rows[i]["Point_distance"].ToString());
                        }
                    }
                    if (dt3.Rows.Count <= 0)
                    {
                        promotion_distance = 0;
                    }
                    else
                    {
                        for (int i = 0; i < dt3.Rows.Count; i++)
                        {
                            promotion_distance += Int32.Parse(dt3.Rows[i]["promotion_item_distance"].ToString());
                        }
                    }
                    sum_distance = point_distance - promotion_distance;
                    Session["sum_distance"] = sum_distance;
                    TextBox4.Text = sum_distance.ToString();
                }
            }
            catch (Exception)
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่มีหมายเลขบัตรประชาชนนี้ในระบบ');", true);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Int32.Parse(Session["sum_distance"].ToString()) < Int32.Parse(Session["sum_choose"].ToString()))
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ระยะทางสะสมไม่เพียงพอ');", true);
            }
            else
            {
                int i = 0;
                foreach (GridViewRow gr in GridView1.Rows)
                {
                    CheckBox CheckBox1 = (CheckBox)gr.FindControl("CheckBox1");
                    if (CheckBox1.Checked == true)
                    {
                        try
                        {
                            Select SData = new Select();
                            Table_store_user x = new Table_store_user();
                            int z = SData.GetId_store_user();
                            x.store_user_id = z;
                            x.Passenger_id = Int32.Parse(Session["user"].ToString());
                            x.promotion_item_id = Int32.Parse(GridView1.Rows[i].Cells[1].Text);
                            db.Table_store_users.InsertOnSubmit(x);                           
                            db.SubmitChanges();
                        }
                        catch (Exception)
                        {
                            ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถบันทึกเสร็จสิ้น');", true);
                        }
                    }
                    i++;
                }
                string distance = @"select Point_distance
                                from Table_Sale
                                inner join Table_Around
                                    on Table_Around.Around_id = Table_Sale.Around_id
                                inner join Table_Route
                                    on Table_Around.Route_id = Table_Route.Route_id
                                inner join Table_Passenger
                                    on Table_Sale.Passenger_id = Table_Passenger.Passenger_id
                                inner join Table_Point
                                    on Table_Sale.Point_id = Table_Point.Point_id                                
                                where Table_Passenger.Passenger_id = '" + Int32.Parse(Session["user"].ToString()) +"' and Table_Sale.SSID = 1";
                string promotion_item = @"select Table_promotion_item.promotion_item_distance
                                    from Table_Passenger
                                    inner join Table_store_user
                                    on Table_Passenger.Passenger_id = Table_store_user.Passenger_id
                                    inner join Table_promotion_item
                                    on Table_promotion_item.promotion_item_id = Table_store_user.promotion_item_id
                                    where Table_Passenger.Passenger_id = '" + Int32.Parse(Session["user"].ToString()) + "' and Table_promotion_item.promotion_item_status = 1";
                SqlCommand sqlcomm2 = new SqlCommand();
                string Connection2 = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                SqlConnection sqlconn2 = new SqlConnection(Connection2);
                SqlDataAdapter da2 = new SqlDataAdapter(distance, sqlconn2);
                SqlDataAdapter da3 = new SqlDataAdapter(promotion_item, sqlconn2);
                DataTable dt2 = new DataTable();
                DataTable dt3 = new DataTable();
                da2.Fill(dt2);
                da3.Fill(dt3);
                if (dt2.Rows.Count <= 0)
                {
                    point_distance = 0;
                }
                else
                {
                    for (int f = 0; f < dt2.Rows.Count; f++)
                    {
                        point_distance += Int32.Parse(dt2.Rows[f]["Point_distance"].ToString());
                    }
                }
                if (dt3.Rows.Count <= 0)
                {
                    promotion_distance = 0;
                }
                else
                {
                    for (int f = 0; f < dt3.Rows.Count; f++)
                    {
                        promotion_distance += Int32.Parse(dt3.Rows[f]["promotion_item_distance"].ToString());
                    }
                }
                sum_distance = point_distance - promotion_distance;
                Session["sum_distance"] = sum_distance;
                TextBox4.Text = sum_distance.ToString();
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('บันทึกเสร็จสิ้น');", true);
                //Response.Redirect(Request.RawUrl);
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Panel2.Visible = false;
            Panel3.Visible = false;
            Session.Remove("sum_distance");
            Session.Remove("sum_choose");
            Session.Remove("user");            
            Response.Redirect(Request.RawUrl);
        }       
        
    }
}